/****************************************************************************/
/*  File:       PackageManagerTextUI.java                                   */
/*  Author:     F. Georges - fgeorges.org                                   */
/*  Date:       2009-06-14                                                  */
/*  Tags:                                                                   */
/*      Copyright (c) 2009 Florent Georges (see end of file.)               */
/* ------------------------------------------------------------------------ */


package org.expath.pkg.saxon;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * Main frontend for all package management actions.
 *
 * @author Florent Georges
 * @date   2009-06-14
 */
public class PackageManagerTextUI
{
    public PackageManagerTextUI(File repo)
            throws FileNotFoundException
    {
        myRepo = new Repository(repo, new TextUserInteraction());
    }

    public void installPackage(File pkg, boolean force)
            throws PackageException
    {
        myRepo.installPackage(pkg, force);
    }

    public static void main(String[] args)
    {
        try {
            // the repo
            String repo = System.getenv("EXPATH_REPO");
            if ( repo == null ) {
                fatalError("$EXPATH_REPO not defined");
            }
            PackageManagerTextUI mgr = new PackageManagerTextUI(new File(repo));

            // options
            if ( args.length == 0 ) {
                usage();
            }
            List<String> options = new ArrayList<String>();
            boolean switch_install = false;
            boolean switch_force   = false;
            for ( String a : args ) {
                if ( "--install".equals(a) || "-i".equals(a) ) {
                    switch_install = true;
                }
                else if ( "--force".equals(a) || "-f".equals(a) ) {
                    switch_force = true;
                }
                // else if ( "--...".equals(a) ) {
                //     ...
                // }
                else if ( a.startsWith("-") ) {
                    fatalError("Unknown option: " + a);
                }
                else {
                    options.add(a);
                }
            }

            // and... action!
            if ( switch_install ) {
                if ( options.size() != 1 ) {
                    fatalError("You must specify exactly one package to install");
                }
                File pkg = new File(options.get(0));
                mgr.installPackage(pkg, switch_force);
            }
            else {
                fatalError("You must select an action (like --install)");
            }
        }
        // TODO: Error management
        catch ( PackageException ex ) {
            ex.printStackTrace();
        }
        catch ( FileNotFoundException ex ) {
            ex.printStackTrace();
        }
    }

    private static void fatalError(String msg)
    {
        System.err.println(msg);
        System.exit(1);
    }

    private static void usage()
    {
        System.err.println("usage:");
        System.err.println("  expath-pkg --install [--force] <pkg>");
        System.exit(1);
    }

    private Repository myRepo;
}


/* ------------------------------------------------------------------------ */
/*  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS COMMENT.               */
/*                                                                          */
/*  The contents of this file are subject to the Mozilla Public License     */
/*  Version 1.0 (the "License"); you may not use this file except in        */
/*  compliance with the License. You may obtain a copy of the License at    */
/*  http://www.mozilla.org/MPL/.                                            */
/*                                                                          */
/*  Software distributed under the License is distributed on an "AS IS"     */
/*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See    */
/*  the License for the specific language governing rights and limitations  */
/*  under the License.                                                      */
/*                                                                          */
/*  The Original Code is: all this file.                                    */
/*                                                                          */
/*  The Initial Developer of the Original Code is Florent Georges.          */
/*                                                                          */
/*  Contributor(s): none.                                                   */
/* ------------------------------------------------------------------------ */
